const { getBusinessInfo, getBusinessList, updateBusinessInfo } = require('../../model/business')
const { getUploadedUrl } = require('../../utils/upload');
/**
 * 获取医疗机构列表
 * 
*/
const businessList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    const map = page && size ? { page, size } : {};
    const result = await getBusinessList(map);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取医疗机构信息
 * 
*/
const businessInfo = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    const info = await getBusinessInfo({id});
    if (info) {
        output.data = info;
    } else {
        output.code = 404;
        output.msg = "企业id错误: 未查询到相关数据";
    }
    res.send(output);
}


/**
 * 更新企业信息
 * 
*/
const businessUpdate = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { id } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    // 获取上传完成的图片地址
    const { file } = req;
    if (file) {
        payload.logo = getUploadedUrl(file);
    }
    const result = await updateBusinessInfo({ ...req.body });
    output.data = result
    res.send(output);
}

module.exports = {
    businessList,
    businessInfo,
    businessUpdate
}